package oop.ex1.filescript;

import java.util.*;

import oop.ex1.filescript.MyFileScript.ActionsNames;
import oop.ex1.filters.Filters;
import oop.ex1.orders.*;

/**
 * an object that contain all the fields if a block of commaneds(Filters, Actions, Orders and Comments)
 * @author danalmog
 * @author danielshalev
 */
public class Blocks {
	private ArrayList<Filters[]> filters;
	private ArrayList<String> actions;
	private String comments;
	private Order order;
	
	/**
	 * constructs a new block
	 */
	public Blocks(){
		this.filters=new ArrayList<Filters[]>();
		this.actions=new ArrayList<String>();
		this.comments=new String();
	}
	
	/**
	 * add's filter to the block
	 * @param filter an array contains the filters of a single line
	 */
	public void addFilter(Filters[] filter){
		this.filters.add(filter);
	}
	
	/**
	 *  add's action to the block
	 * @param action the action to add
	 * @throws BadActionExeption indicates that there is a problem with one of the actions
	 */
	public void addAction(String action) throws BadActionExeption{
		try{
			ActionsNames.valueOf(action.split("%")[0]);
		}
		catch(java.lang.IllegalArgumentException e){
			throw new BadActionExeption();
		}
		finally{
			this.actions.add(action);
		}
	}
	
	/**
	 * adds a comment
	 * @param comment the comment to add
	 */
	public void addComment(String comment){
		this.comments+=comment + "\n";
	}
	
	/**
	 * set the order that the files should be arranged by
	 * @param order the order to add
	 */
	public void setOrder(Order order){
		this.order=order;
	}
	
	/**
	 * a getter that returns the filters list
	 * @return arraylist of filters
	 */
	public ArrayList<Filters[]> getFilters(){
		return filters;
	}

	/**
	 * a getter that returns the actions list
	 * @return an arraylist of actions
	 */
	public ArrayList<String> getActions(){
		return actions;
	}
	
	/**
	 * a getter that returns the order which the files should be arranged by
	 * @return the order which the files should be arranged by
	 */
	public Order getOreder(){
		return order;
	}
	
	/**
	 * a getter thats return the comments 
	 * @return thats return the comments
	 */
	public String getComments(){
		if (comments.length()>0){
			if(comments.endsWith(" "))
				comments=comments.trim();
			return comments.substring(0, comments.lastIndexOf("\n"));
		}
		return null;
	}
}
